package tests;

import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.IOException;
import java.time.Duration;
import java.util.Set;

public class ProjectPage extends Utils {
    public static String url = "http://8.155.1.153:8081/project";
    public ProjectPage() {
        super(url);
    }

    public void checkPageRight() throws IOException {
        //查看页面关键元素
        //检查新建按钮，搜索框是否加载成功
        driver.findElement(By.xpath("//*[@id=\"sk-layout\"]/div/div/section/aside/div/div[1]/ul/li[2]")).click();
        driver.findElement(By.xpath("//*[@id=\"sk-layout\"]/div/div/section/div[2]/main/div/div[1]/div[1]/span/div/div/button"));
        driver.findElement(By.xpath("//*[@id=\"sk-layout\"]/div/div/section/div[2]/main/div/div[1]/div[2]/div/div/div/div/div/div/div/span/span/input"));
        //检查项目是否加载成功
        driver.findElement(By.xpath("//*[@id=\"sk-layout\"]/div/div/section/div[2]/main/div/div[2]/div/div/div/div[1]/div/div/div/div[1]/div/div/div/div"));
        driver.findElement(By.xpath("//*[@id=\"sk-layout\"]/div/div/section/div[2]/main/div/div[2]/div/div/div/div[1]/div/div/div/div[2]/div/div/div/div"));
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
    }

    public void NewQuestionnaire() throws IOException {
        driver.findElement(By.xpath("//*[@id=\"sk-layout\"]/div/div/section/div/main/div/div[1]/div[1]/span/div/div/button")).click();
        driver.findElement(By.xpath("/html/body/div[4]/div/div/ul/li[1]/span")).click();
        driver.findElement(By.xpath("//*[@id=\"editorContent\"]/div/div/div[1]/div[2]/div[1]/div/pre/div[1]")).clear();
        driver.findElement(By.xpath("//*[@id=\"editorContent\"]/div/div/div[1]/div[2]/div[1]/div/pre/div[1]")).sendKeys("一个新问卷");
        driver.findElement(By.xpath("//*[@id=\"rc-tabs-0-panel-1\"]/div/div/dl[1]/div/div[1]/dd/div")).click();
        driver.findElement(By.xpath("//*[@id=\"editorContent\"]/div/div/div[2]/div/div/div/div[1]/div[1]/div[2]/div/pre/div[1]")).clear();
        driver.findElement(By.xpath("//*[@id=\"editorContent\"]/div/div/div[2]/div/div/div/div[1]/div[1]/div[2]/div/pre/div[1]")).sendKeys("你是学生吗？");
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        WebElement element=wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"editorContent\"]/div/div/div[2]/div/div/div/div[1]/div[3]/div/div/div[1]/div/div/div[1]/div[1]/div/pre/div[1]")));
        driver.findElement(By.xpath("//*[@id=\"editorContent\"]/div/div/div[2]/div/div/div/div[1]/div[3]/div/div/div[1]/div/div/div[1]/div[1]/div/pre/div[1]")).sendKeys("是");
        WebElement element1=wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"editorContent\"]/div/div/div[2]/div/div/div/div[1]/div[3]/div/div/div[2]/div/div/div[1]/div[1]/div/pre/div[1]")));
        driver.findElement(By.xpath("//*[@id=\"editorContent\"]/div/div/div[2]/div/div/div/div[1]/div[3]/div/div/div[2]/div/div/div[1]/div[1]/div/pre/div[1]")).sendKeys("否");
        driver.findElement(By.xpath("//*[@id=\"editor\"]/div[2]/div[1]/div[2]/div/button[4]/span")).click();
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
        driver.navigate().back();
        driver.navigate().back();
        driver.findElement(By.xpath("//*[@id=\"sk-layout\"]/div/div/section/aside/div/div[1]/ul/li[2]")).click();

        WebDriverWait wait1 = new WebDriverWait(driver, Duration.ofSeconds(10));
        wait1.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"sk-layout\"]/div/div/section/div[2]/main/div/div[2]/div/div/div/div[1]/div/div/div/div[3]/div/div/div/div/div/div[1]/span[1]")));
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());

    }

    public void NewExam() throws InterruptedException, IOException {
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));

        driver.findElement(By.xpath("//*[@id=\"sk-layout\"]/div/div/section/div[2]/main/div/div[1]/div[1]/span/div/div/button")).click();
        driver.findElement(By.xpath("/html/body/div[5]/div/div/ul/li[2]/span")).click();
        driver.findElement(By.xpath("//*[@id=\"editorContent\"]/div/div/div[1]/div[2]/div[1]/div/pre/div[1]")).clear();
        driver.findElement(By.xpath("//*[@id=\"editorContent\"]/div/div/div[1]/div[2]/div[1]/div/pre/div[1]")).sendKeys("一个新试卷");
        WebDriverWait wait2 = new WebDriverWait(driver, Duration.ofSeconds(10));
        WebElement button = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"rc-tabs-3-panel-1\"]/div/div/dl[1]/div/div[1]/dd/div")));
        button.click();

        driver.findElement(By.xpath("//*[@id=\"editorContent\"]/div/div/div[2]/div/div/div/div[1]/div[1]/div[2]/div/pre/div[1]")).clear();
        driver.findElement(By.xpath("//*[@id=\"editorContent\"]/div/div/div[2]/div/div/div/div[1]/div[1]/div[2]/div/pre/div[1]/p")).sendKeys("中国有多少个自治区？");
        //WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        //WebElement element=wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"editorContent\"]/div/div/div[2]/div/div/div/div[1]/div[3]/div/div/div[1]/div/div/div[1]/div[1]/div/pre/div[1]")));
        driver.findElement(By.xpath("//*[@id=\"editorContent\"]/div/div/div[2]/div/div/div/div[1]/div[3]/div/div/div[1]/div/div/div[1]/div[1]/div/pre/div[1]")).sendKeys("5");

        WebElement element1=wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"editorContent\"]/div/div/div[2]/div/div/div/div[1]/div[3]/div/div/div[2]/div/div/div[1]/div[1]/div/pre/div[1]")));

        Actions actions = new Actions(driver);
        actions.click(element1) // 先点击元素获取焦点
                .keyDown(Keys.CONTROL) // 按下Ctrl
                .sendKeys("a") // Ctrl+A 全选
                .keyUp(Keys.CONTROL) // 释放Ctrl
                .sendKeys(Keys.DELETE) // 删除选中的文本
                .sendKeys("6") // 输入新文本
                .perform();

        /*driver.findElement(By.xpath("//*[@id=\\\"editorContent\\\"]/div/div/div[2]/div/div/div/div[1]/div[3]/div/div/div[2]/div/div[1]/div[1]")).click();
        driver.findElement(By.xpath("//*[@id=\\\"editorContent\\\"]/div/div/div[2]/div/div/div/div[1]/div[3]/div/div/div[2]/div/div[1]/div[1]")).sendKeys("6");*/
        driver.findElement(By.xpath("//*[@id=\"editor\"]/div[2]/div[1]/div[2]/div/button[5]/span")).click();
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
        driver.navigate().to("http://8.155.1.153:8081/home");
        Thread.sleep(3000);
        driver.findElement(By.xpath("//*[@id=\"sk-layout\"]/div/div/section/aside/div/div[1]/ul/li[2]/span/a")).click();
        WebDriverWait wait1 = new WebDriverWait(driver, Duration.ofSeconds(10));
        wait1.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"sk-layout\"]/div/div/section/div[2]/main/div/div[2]/div/div/div/div[1]/div/div/div/div[3]/div/div/div/div/div/div[1]/span[1]")));
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
    }
    public void DeleteProject() throws IOException {
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));

        driver.findElement(By.xpath("//*[@id=\"sk-layout\"]/div/div/section/div[2]/main/div/div[2]/div/div/div/div[1]/div/div/div/div[4]/div/div/div/ul/li[5]/span/button")).click();
        driver.findElement(By.xpath("/html/body/div[4]/div/div/ul/li[9]")).click();
        WebElement deleteBtn = driver.findElement(By.xpath("//div[@class='ant-modal-confirm-btns']//button[span[text()='删 除']]"));
        WebDriverWait wait1 = new WebDriverWait(driver, Duration.ofSeconds(10));
        wait1.until(ExpectedConditions.elementToBeClickable(By.xpath("/html/body/div[5]/div/div[2]/div/div[2]/div/div/div[2]/button[2]")));
        deleteBtn.click();
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        wait.until(ExpectedConditions.invisibilityOfElementLocated(
                By.xpath("//*[@id=\"sk-layout\"]/div/div/section/div[2]/main/div/div[2]/div/div/div/div[1]/div/div/div/div[4]/div/div/div/div/div/div[1]/span[1]")));
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
    }
    public void SearchProject() throws InterruptedException, IOException {
        driver.findElement(By.xpath("//*[@id=\"sk-layout\"]/div/div/section/div[2]/main/div/div[1]/div[2]/div/div/div/div/div/div/div/span/span/input")).sendKeys("大学生问卷调查");
        driver.findElement(By.xpath("//*[@id=\"sk-layout\"]/div/div/section/div[2]/main/div/div[1]/div[2]/div/div/div/div/div/div/div/span/span/span/button")).click();
        driver.findElement(By.xpath("//*[@id=\"sk-layout\"]/div/div/section/div[2]/main/div/div[2]/div/div/div/div[1]/div/div/div/div/div/div/div/div/div/div[1]/span[1]"));
        Actions action=new Actions(driver);
        WebElement ele= driver.findElement(By.xpath("//*[@id=\"sk-layout\"]/div/div/section/div[2]/main/div/div[1]/div[2]/div/div/div/div/div/div/div/span/span"));
        action.sendKeys(ele, Keys.HOME) // 将光标移动到输入框开头
                .keyDown(Keys.SHIFT) // 按下Shift键
                .sendKeys(Keys.END) // 选中从光标位置到结尾的所有文本
                .keyUp(Keys.SHIFT) // 释放Shift键
                .sendKeys(Keys.DELETE) // 删除选中的文本
                .perform();
        driver.findElement(By.xpath("//*[@id=\"sk-layout\"]/div/div/section/div[2]/main/div/div[1]/div[2]/div/div/div/div/div/div/div/span/span/span/button")).click();
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());

    }
    public void FinishExams() throws InterruptedException, IOException {
        driver.findElement(By.xpath("//*[@id=\"sk-layout\"]/div/div/section/div[2]/main/div/div[2]/div/div/div/div[1]/div/div/div/div[2]/div/div/div/div/div")).click();
        driver.findElement(By.xpath("//*[@id=\"root\"]/div/div[2]/div/div/div[2]/div[2]/div[1]/div/div/div[2]/div[2]/button/div")).click();
        driver.findElement(By.xpath("//*[@id=\"root\"]/div/div[2]/div/div/div[2]/div[2]/div[1]/div/div/div[1]/span[1]/span/span/button")).click();
         String curwindow=driver.getWindowHandle();//当前句柄
        Set<String> allWindow =driver.getWindowHandles();
        for(String w:allWindow){
            if(w!=curwindow){
                driver.switchTo().window(w);
            }
        }
        driver.findElement(By.xpath("//*[@id=\"root\"]/div/div/div/div[3]/div[1]/div"));
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
    }
}
